package com.cory.module.job.core;

import com.cory.boot.utils.SpringUtils;
import com.cory.module.job.core.annotation.AsyncJob;
import com.cory.module.job.core.annotation.ExcelExportJob;
import com.cory.module.job.core.annotation.ExcelImportJob;
import com.cory.module.job.core.handle.AsyncJobHandle;
import com.cory.module.job.core.handle.ExcelExportJobHandle;
import com.cory.module.job.core.handle.ExcelImportJobHandle;
import com.google.common.collect.Maps;
import lombok.Getter;

import java.util.Collection;
import java.util.Map;

/**
 * @author xiongcong
 * @date 2023-05-09
 */
public class JobFactory {

    private static Map<String, AsyncJobHandle> asyncJobHandleMap = Maps.newHashMap();
    @Getter
    private static Map<String, AsyncJob> asyncJobAnnotationMap = Maps.newHashMap();
    
    private static Map<String, ExcelExportJobHandle<?, ?>> excelExportJobHandleMap = Maps.newHashMap();

    private static Map<String, ExcelImportJobHandle<?>> excelImportJobHandleMap = Maps.newHashMap();
    @Getter
    private static Map<String, ExcelImportJob> excelImportJobAnnotationMap = Maps.newHashMap();
    
    static {
        Collection<AsyncJobHandle> beans = SpringUtils.getApplicationContext().getBeansOfType(AsyncJobHandle.class).values();
        for (AsyncJobHandle bean : beans) {
            AsyncJob annotation = bean.getClass().getAnnotation(AsyncJob.class);
            if (annotation != null) {
                asyncJobHandleMap.put(annotation.type(), bean);
                asyncJobAnnotationMap.put(annotation.type(), annotation);
            }
        }
    }
    
    static {
        @SuppressWarnings("rawtypes")
        Collection<ExcelExportJobHandle> beans = SpringUtils.getApplicationContext().getBeansOfType(ExcelExportJobHandle.class).values();
        for (ExcelExportJobHandle<?, ?> bean : beans) {
            ExcelExportJob annotation = bean.getClass().getAnnotation(ExcelExportJob.class);
            if (annotation != null) {
                bean.setExportMode(annotation.exportMode());
                excelExportJobHandleMap.put(annotation.type(), bean);
            }
        }
    }

    static {
        @SuppressWarnings("rawtypes")
        Collection<ExcelImportJobHandle> beans = SpringUtils.getApplicationContext().getBeansOfType(ExcelImportJobHandle.class).values();
        for (ExcelImportJobHandle<?> bean : beans) {
            ExcelImportJob annotation = bean.getClass().getAnnotation(ExcelImportJob.class);
            if (annotation != null) {
                excelImportJobHandleMap.put(annotation.type(), bean);
                excelImportJobAnnotationMap.put(annotation.type(), annotation);
            }
        }
    }
    
    public static AsyncJobHandle getAsyncJobHandle(String type) {
        return asyncJobHandleMap.get(type);
    }

    public static AsyncJob getAsyncJobAnnotation(String type) {
        return asyncJobAnnotationMap.get(type);
    }
    
    public static ExcelExportJobHandle<?, ?> getExcelExportJobHandle(String type) {
        return excelExportJobHandleMap.get(type);
    }

    public static ExcelImportJobHandle<?> getExcelImportJobHandle(String type) {
        return excelImportJobHandleMap.get(type);
    }

    public static ExcelImportJob getExcelImportJobAnnotation(String type) {
        return excelImportJobAnnotationMap.get(type);
    }
}
